#! /bin/sh /usr/share/dpatch/dpatch-run
## transcode_profiles.dpatch by Jean-Yves Avenard <jean-yves@avenard.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

@DPATCH@
diff -urNad mythtv-0.22.0-fixes22840~/libs/libmythtv/recordinginfo.cpp mythtv-0.22.0-fixes22840/libs/libmythtv/recordinginfo.cpp
--- mythtv-0.22.0-fixes22840~/libs/libmythtv/recordinginfo.cpp	2009-10-15 03:32:47.000000000 +0200
+++ mythtv-0.22.0-fixes22840/libs/libmythtv/recordinginfo.cpp	2009-11-17 01:40:08.000000000 +0100
@@ -298,6 +298,27 @@
     subtitle = newSubtitle;
 }
 
+/* \fn RecordingInfo::ApplyTranscoderProfileChangeById(int id)
+ * \brief Sets the transcoder profile for a recording
+ * \param profileid is the 'id' field from recordingprofiles table.
+ */
+void RecordingInfo::ApplyTranscoderProfileChangeById(int id)
+{
+    MSqlQuery query(MSqlQuery::InitCon());
+
+    query.prepare("UPDATE recorded "
+            "SET transcoder = :PROFILEID "
+            "WHERE chanid = :CHANID "
+            "AND starttime = :START");
+    query.bindValue(":PROFILEID",  id);
+    query.bindValue(":CHANID",  chanid);
+    query.bindValue(":START",  recstartts);
+
+    if (!query.exec())
+        MythDB::DBError(LOC + "unable to update transcoder "
+                "in recorded table", query);
+}
+
 /** \brief Sets the transcoder profile for a recording
  *  \param profile Descriptive name of the profile. ie: Autodetect
  */
diff -urNad mythtv-0.22.0-fixes22840~/libs/libmythtv/recordinginfo.h mythtv-0.22.0-fixes22840/libs/libmythtv/recordinginfo.h
--- mythtv-0.22.0-fixes22840~/libs/libmythtv/recordinginfo.h	2009-10-15 03:32:51.000000000 +0200
+++ mythtv-0.22.0-fixes22840/libs/libmythtv/recordinginfo.h	2009-11-17 01:40:08.000000000 +0100
@@ -68,6 +68,7 @@
     void ApplyRecordRecTitleChange(const QString &newTitle,
                                    const QString &newSubtitle);
     void ApplyTranscoderProfileChange(const QString &profile) const;//pi
+    void ApplyTranscoderProfileChangeById(int);
 
     static void signalChange(int recordid);
 
diff -urNad mythtv-0.22.0-fixes22840~/programs/mythfrontend/playbackbox.cpp mythtv-0.22.0-fixes22840/programs/mythfrontend/playbackbox.cpp
--- mythtv-0.22.0-fixes22840~/programs/mythfrontend/playbackbox.cpp	2009-11-13 08:33:54.000000000 +0100
+++ mythtv-0.22.0-fixes22840/programs/mythfrontend/playbackbox.cpp	2009-11-17 01:40:08.000000000 +0100
@@ -2748,27 +2748,64 @@
     if (!(m_popupMenu = createPopupMenu(tr("Transcoding profiles"))))
         return;
 
-    m_popupMenu->AddButton(tr("Default"), SLOT(doBeginTranscoding()));
-    m_popupMenu->AddButton(tr("Autodetect"),
-                                    SLOT(changeProfileAndTranscodeAuto()));
-    m_popupMenu->AddButton(tr("High Quality"),
-                                    SLOT(changeProfileAndTranscodeHigh()));
-    m_popupMenu->AddButton(tr("Medium Quality"),
-                                    SLOT(changeProfileAndTranscodeMedium()));
-    m_popupMenu->AddButton(tr("Low Quality"),
-                                    SLOT(changeProfileAndTranscodeLow()));
+    int buttonCount = kDialogCodeListStart;
+    m_transcodeProfileMap.clear();
+
+    m_popupMenu->AddButton(tr("Default"),qVariantFromValue(buttonCount));
+    m_transcodeProfileMap.insert(buttonCount++, -1);
+
+    m_popupMenu->AddButton(tr("Autodetect"),qVariantFromValue(buttonCount));
+    m_transcodeProfileMap.insert(buttonCount++, 0);
+
+    MSqlQuery query(MSqlQuery::InitCon());
+    query.prepare("SELECT r.name, r.id "
+            "FROM recordingprofiles r, profilegroups p "
+            "WHERE p.name = 'Transcoders' "
+            "AND r.profilegroup = p.id "
+            "AND r.name != 'RTjpeg/MPEG4' "
+            "AND r.name != 'MPEG2' ");
+
+    if (!query.exec())
+    {
+        MythDB::DBError(LOC + "unable to query transcoders", query);
+        return;
+    }
+
+    while (query.next())
+    {
+        QString transcoder_name = query.value(0).toString();
+        int transcoder_id = query.value(1).toInt();
+
+        // Translatable strings for known profiles
+        if (transcoder_name == "High Quality")
+            m_popupMenu->AddButton(tr("High Quality"),qVariantFromValue(buttonCount));
+        else if (transcoder_name == "Medium Quality")
+            m_popupMenu->AddButton(tr("Medium Quality"),qVariantFromValue(buttonCount));
+        else if (transcoder_name == "Low Quality")
+            m_popupMenu->AddButton(tr("Low Quality"),qVariantFromValue(buttonCount));
+        else
+            m_popupMenu->AddButton(transcoder_name,qVariantFromValue(buttonCount));
+        m_transcodeProfileMap.insert(buttonCount++, transcoder_id);
+    }
+    m_popupMenu->SetReturnEvent(this, "TRANSCODE");
 }
 
-void PlaybackBox::changeProfileAndTranscode(const QString &profile)
+void PlaybackBox::changeProfileAndTranscode(int id)
 {
-   ProgramInfo *pginfo = CurrentItem();
+    ProgramInfo *pginfo = CurrentItem();
 
     if (!pginfo)
         return;
 
-    const RecordingInfo ri(*pginfo);
-    ri.ApplyTranscoderProfileChange(profile);
-    doBeginTranscoding();
+    if (m_transcodeProfileMap.contains(id))
+    {
+        if (m_transcodeProfileMap.value(id) >= 0)
+        {
+            RecordingInfo ri(*pginfo);
+            ri.ApplyTranscoderProfileChangeById(m_transcodeProfileMap.value(id));
+        }
+        doBeginTranscoding();
+    }
 }
 
 void PlaybackBox::showActionPopup(ProgramInfo *pginfo)
@@ -3600,6 +3637,20 @@
 
 void PlaybackBox::customEvent(QEvent *event)
 {
+    if (event->type() == kMythDialogBoxCompletionEventType)
+    {
+        DialogCompletionEvent *dce =
+        dynamic_cast<DialogCompletionEvent*>(event);
+        
+        QString resultid= dce->GetId();
+        
+        if (resultid == "TRANSCODE")
+        {
+            int profileid = dce->GetData().toInt();
+            changeProfileAndTranscode(profileid);
+        }
+    }
+
     if ((MythEvent::Type)(event->type()) == MythEvent::MythEventMessage)
     {
         MythEvent *me = (MythEvent *)event;
diff -urNad mythtv-0.22.0-fixes22840~/programs/mythfrontend/playbackbox.h mythtv-0.22.0-fixes22840/programs/mythfrontend/playbackbox.h
--- mythtv-0.22.0-fixes22840~/programs/mythfrontend/playbackbox.h	2009-10-24 08:40:02.000000000 +0200
+++ mythtv-0.22.0-fixes22840/programs/mythfrontend/playbackbox.h	2009-11-17 01:40:08.000000000 +0100
@@ -158,15 +158,7 @@
     void showRecordingPopup();
     void showJobPopup();
     void showTranscodingProfiles();
-    void changeProfileAndTranscode(const QString &profile);
-    void changeProfileAndTranscodeAuto()
-             { changeProfileAndTranscode("Autodetect"); }
-    void changeProfileAndTranscodeHigh()
-             { changeProfileAndTranscode("High Quality"); }
-    void changeProfileAndTranscodeMedium()
-             { changeProfileAndTranscode("Medium Quality"); }
-    void changeProfileAndTranscodeLow()
-             { changeProfileAndTranscode("Low Quality"); }
+    void changeProfileAndTranscode(int id);
     void showStoragePopup();
     void showPlaylistPopup();
     void showPlaylistStoragePopup();
@@ -452,6 +444,9 @@
     deque<QString>      m_networkControlCommands;
     bool                m_underNetworkControl;
 
+    // Transcoding Profiles Variables//////////////////////////////////////////
+    QMap<int,int>       m_transcodeProfileMap;
+
     TV                  *m_player;
 };
 
